package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.*;
import cc.rengu.igas.bomp.common.dao.impl.*;
import cc.rengu.igas.bomp.common.entity.*;
import cc.rengu.igas.bomp.common.enums.QueryBizTypeEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntProdInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntProdListQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntProdListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户产品列表查询
 * Created by 赵奎伍
 */
public class MchntProdListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntProdListQueryRequest MchntProdListQueryRequest = new MchntProdListQueryRequest();
        ConvertUtil.convertOutput(MchntProdListQueryRequest);
        return MchntProdListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntProdListQueryRequest mchntProdListQueryRequest = (MchntProdListQueryRequest) request;
        MchntProdListQueryResponse mchntProdListQueryResponse = new MchntProdListQueryResponse();
        BeanUtil.beanCopy(mchntProdListQueryRequest, mchntProdListQueryResponse);
        BizResponse bizResponse = new BizResponse();
        List<MchntProdInfo> mchntProdInfoList = new ArrayList<>();
        String bizType = mchntProdListQueryRequest.getHeader().getBizType();
        if (StringUtil.isEmptyOrNull(bizType)) {
            rglog.error("bizType<{}>为NULL!,无法查询信息", bizType);
            bizResponse.setRespCode(RespCodeEnum.PARAM_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.PARAM_ERROR.getRespDesc());
            bizResponse.setResult(mchntProdListQueryResponse);
            return bizResponse;
        }

        if (QueryBizTypeEnum.QUERY_TEMPORARY_TABLE.getType().equals(bizType)) {
            /* 查询临时表数据 */

            BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
            List<BizTmp> mchntProdBizTmpList = bizTmpMapper.selectBizTmpByMchntNo(mchntProdListQueryRequest.getHeader().getInstId(), mchntProdListQueryRequest.getMchntNo(), TableNameConstant.T_B_MCHNT_PROD_INFO);

            MchntProdInfoMapper mchntProdInfoMapper = new MchntProdInfoMapperImpl();
            mchntProdInfoList = mchntProdInfoMapper.selectMchntProdInfoByMchntNo(mchntProdListQueryRequest.getHeader().getInstId(), mchntProdListQueryRequest.getMchntNo());
            if (!CollectionUtils.isEmpty(mchntProdBizTmpList) && !CollectionUtils.isEmpty(mchntProdInfoList)
                    && mchntProdBizTmpList.size() >= mchntProdInfoList.size()) {
                MchntProdInfo mchntProdInfo = null;
                BaseProdInfoMapper baseProdInfoMapper = new BaseProdInfoMapperImpl();
                List<MchntProdInfoBean> mchntProdInfoBeans = new ArrayList<>();
                for (BizTmp bizTmp : mchntProdBizTmpList) {
                    MchntProdInfoBean mchntProdInfoBean = new MchntProdInfoBean();
                    String mchntProdInfoJson = InitParamUtil.getJsonString(bizTmp);
                    mchntProdInfo = JSON.parseObject(mchntProdInfoJson, MchntProdInfo.class);

                    if (AppParamConstant.NO.equals(mchntProdInfo.getRecordStatus())) {
                        //失效产品不展示
                        continue;
                    }

                    if (!"DF".equals(mchntProdInfo.getProdCode())) {
                        ProdInfoMapper prodInfoMapper = new ProdInfoMapperImpl();
                        ProdInfo prodInfo = prodInfoMapper.selectProdInfoByPrimaryKey(mchntProdInfo.getInstId(), mchntProdInfo.getProdCode(), mchntProdInfo.getBaseProdCode());
                        if (null != prodInfo) {
                            mchntProdInfoBean.setProdName(prodInfo.getProdName());
                        }
                    }
                    BaseProdInfo baseProdInfo = baseProdInfoMapper.selectBaseProdInfoByPrimaryKey(mchntProdInfo.getInstId(), mchntProdInfo.getBaseProdCode());


                    BeanUtil.beanCopy(mchntProdInfo, mchntProdInfoBean);
                    mchntProdInfoBean.setBaseProdPath(mchntProdInfo.getProdPath());
                    if (null != baseProdInfo) {
                        mchntProdInfoBean.setBaseProdName(baseProdInfo.getBaseProdName());
                        mchntProdInfoBean.setBaseProdDesc(baseProdInfo.getBaseProdDesc());
                    }

                    mchntProdInfoBeans.add(mchntProdInfoBean);
                }
                mchntProdInfoBeans = mchntProdInfoBeans.stream().sorted(Comparator.comparing(MchntProdInfoBean::getBaseProdCode)).collect(Collectors.toList());
                /* 查询结果赋值 */
                mchntProdListQueryResponse.setMchntProdInfoList(mchntProdInfoBeans);
                List<BizTmp> mchntBaseBizTmpList = bizTmpMapper.selectBizTmpByMchntNo(mchntProdListQueryRequest.getHeader().getInstId(), mchntProdListQueryRequest.getMchntNo(), TableNameConstant.T_B_MCHNT_BASE_INFO);
                if (!CollectionUtils.isEmpty(mchntBaseBizTmpList)) {

                    BizTmp bizTmp = mchntBaseBizTmpList.get(0);
                    String mchntBaseJsonInfo = InitParamUtil.getJsonString(bizTmp);
                    MchntBaseInfo mchntBaseInfo = JSON.parseObject(mchntBaseJsonInfo, MchntBaseInfo.class);

                    /* 查询结果赋值 */
                    mchntProdListQueryResponse.setIp(mchntBaseInfo.getIp());
                    mchntProdListQueryResponse.setIcpRecordNo(mchntBaseInfo.getIcpRecordNo());
                    mchntProdListQueryResponse.setMchntNetworkDomain(mchntBaseInfo.getMchntNetworkDomain());
                }
                mchntProdListQueryResponse.setMchntNo(mchntProdListQueryRequest.getMchntNo());
            } else {
                //查询正式表，兼容迁移数据
                rglog.error("商户号<{}>，临时表无商户产品信息!，查询正式表！", mchntProdListQueryResponse.getMchntNo());
                getMchntProdListInfoByFormalTable(mchntProdListQueryRequest, mchntProdListQueryResponse);
            }
        } else {
            //查询正式表
            getMchntProdListInfoByFormalTable(mchntProdListQueryRequest, mchntProdListQueryResponse);
        }

        /* 按照接口转换基础产品信息并响应给请求方 */

        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntProdListQueryResponse);
        return bizResponse;
    }

    private void getMchntProdListInfoByFormalTable(MchntProdListQueryRequest mchntProdListQueryRequest, MchntProdListQueryResponse mchntProdListQueryResponse) throws Exception {
        List<MchntProdInfo> mchntProdInfoList;/* 查询正式表数据 */
        MchntProdInfoMapper mchntProdInfoMapper = new MchntProdInfoMapperImpl();
        mchntProdInfoList = mchntProdInfoMapper.selectMchntProdInfoByMchntNo(mchntProdListQueryRequest.getHeader().getInstId(), mchntProdListQueryRequest.getMchntNo());
        if (!CollectionUtils.isEmpty(mchntProdInfoList)) {
            /* 按照创建时间降序排序 */
            mchntProdInfoList = mchntProdInfoList.stream().sorted(Comparator.comparing(MchntProdInfo::getCreateTime)).collect(Collectors.toList());
            Collections.reverse(mchntProdInfoList);
            BaseProdInfoMapper baseProdInfoMapper = new BaseProdInfoMapperImpl();
            List<MchntProdInfoBean> mchntProdInfoBeans = new ArrayList<>();
            for (MchntProdInfo mchntProdInfo : mchntProdInfoList) {

                if (AppParamConstant.NO.equals(mchntProdInfo.getRecordStatus())) {
                    //失效产品不展示
                    continue;
                }

                MchntProdInfoBean mchntProdInfoBean = new MchntProdInfoBean();
                if (!"DF".equals(mchntProdInfo.getProdCode())) {
                    ProdInfoMapper prodInfoMapper = new ProdInfoMapperImpl();
                    ProdInfo prodInfo = prodInfoMapper.selectProdInfoByPrimaryKey(mchntProdInfo.getInstId(), mchntProdInfo.getProdCode(), mchntProdInfo.getBaseProdCode());
                    if (null != prodInfo) {
                        mchntProdInfoBean.setProdName(prodInfo.getProdName());
                    }
                }

                BeanUtil.beanCopy(mchntProdInfo, mchntProdInfoBean);
                mchntProdInfoBean.setBaseProdPath(mchntProdInfo.getProdPath());
                BaseProdInfo baseProdInfo = baseProdInfoMapper.selectBaseProdInfoByPrimaryKey(mchntProdInfo.getInstId(), mchntProdInfo.getBaseProdCode());
                if (null != baseProdInfo) {
                    mchntProdInfoBean.setBaseProdName(baseProdInfo.getBaseProdName());
                    mchntProdInfoBean.setBaseProdDesc(baseProdInfo.getBaseProdDesc());
                }

                mchntProdInfoBeans.add(mchntProdInfoBean);
            }

            mchntProdInfoBeans = mchntProdInfoBeans.stream().sorted(Comparator.comparing(MchntProdInfoBean::getBaseProdCode)).collect(Collectors.toList());

            /* 查询结果赋值 */
            mchntProdListQueryResponse.setMchntProdInfoList(mchntProdInfoBeans);
        } else {
            rglog.error("法人行<{}>无商户产品信息!", mchntProdListQueryRequest.getHeader().getInstId());
        }

        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(mchntProdListQueryRequest.getHeader().getInstId(), mchntProdListQueryRequest.getMchntNo());
        if (null != mchntBaseInfo) {
            mchntProdListQueryResponse.setIp(mchntBaseInfo.getIp());
            mchntProdListQueryResponse.setIcpRecordNo(mchntBaseInfo.getIcpRecordNo());
            mchntProdListQueryResponse.setMchntNetworkDomain(mchntBaseInfo.getMchntNetworkDomain());
        }
        mchntProdListQueryResponse.setMchntNo(mchntProdListQueryRequest.getMchntNo());
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntProdListQueryResponse MchntProdListQueryResponse = (MchntProdListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(MchntProdListQueryResponse);
    }

}
